package actions;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.NewsInfo;

import org.apache.struts2.ServletActionContext;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import util.DateUtil;
import util.KeywordUtil;

import com.opensymphony.xwork2.ActionSupport;

import daoImp.*;
import dao.*;

public class TrendsAction extends ActionSupport{
	
	private HttpServletRequest request;
	private HttpServletResponse response;
	private TrendsDAO trendsDAO = TrendsDAOImp.getInstance();
	private NewsDAO newsDAO = NewsDAOImp.getInstance();
	private NewsKeywordsDAO newsKeywordsDAO = NewsKeywordsDAOImp.getInstance();
	private SearchKeywordDAO searchKeywordsDAO = SearchKeywordDAOImp.getInstance();
	private TweetEmotionDAO tweetEmotionDAO = TweetEmotionDAOImp.getInstance();
	private TrendFrequencyDAO trendFrequencyDAO = TrendFrequencyDAOImpl.getInstance();
	private PhotoDAO photoDAO = PhotoDAOImpl.getInstance();

	static{
		try {
			ConnectionPool.getInstance().createPool();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public TrendsAction(){
		request = ServletActionContext.getRequest();
		response = ServletActionContext.getResponse();
	}
	
	public String getAllTrends(){
		response.setContentType(" text/xml ");
		int year = Integer.parseInt(request.getParameter("year"));
		int month = Integer.parseInt(request.getParameter("month"));
		int day = Integer.parseInt(request.getParameter("day"));
		int hour = Integer.parseInt(request.getParameter("hour"));
		Date date = DateUtil.getDate(year,month,day,hour);
		ArrayList<String> trendsList = trendsDAO.getUsefulTrendsByDate(date);
		JSONArray jsonArray = new JSONArray();
		for(String trend : trendsList){
			jsonArray.add(trend);
		}
		try {
			response.getWriter().print(jsonArray);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	public String getPhotoInfo(){
		response.setContentType(" text/xml ");
		String trend = request.getParameter("trend");
		int year = Integer.parseInt(request.getParameter("year"));
		int month = Integer.parseInt(request.getParameter("month"));
		int day = Integer.parseInt(request.getParameter("day"));
		int hour = Integer.parseInt(request.getParameter("hour"));
		Date date = new Date(year-1900,month-1,day);
		String info = photoDAO.getPhotoInfo(trend, date);
		try {
			response.getWriter().print(info);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	
	public String getRecentTrendFrequency(){
		response.setContentType(" text/xml ");
		String trend = request.getParameter("trend");
		int year = Integer.parseInt(request.getParameter("year"));
		int month = Integer.parseInt(request.getParameter("month"));
		int day = Integer.parseInt(request.getParameter("day"));
		int hour = Integer.parseInt(request.getParameter("hour"));
		Date date = new Date(year-1900,month-1,day);
		int[] frequencies = trendFrequencyDAO.getRecentFrequency(trend, date, 7);
		JSONArray jsonArray = new JSONArray();
		for(Integer frequency : frequencies){
			jsonArray.add(frequency);
		}
		try {
			response.getWriter().print(jsonArray);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	public String getSearchKeywords(){
		response.setContentType(" text/xml ");
		String trend = request.getParameter("trend");
		int year = Integer.parseInt(request.getParameter("year"));
		int month = Integer.parseInt(request.getParameter("month"));
		int day = Integer.parseInt(request.getParameter("day"));
		Date date = new Date(year-1900,month-1,day);
		ArrayList<String> keywords = searchKeywordsDAO.getKeywordsByTrend(trend, date);
		JSONArray keywordsArray = new JSONArray();
		for(String keyword : keywords){
			keywordsArray.add(keyword);
		}
	
		try {
			response.getWriter().print(keywordsArray);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	public String getEmotions(){
		response.setContentType(" text/xml ");
		String trend = request.getParameter("trend");
		int year = Integer.parseInt(request.getParameter("year"));
		int month = Integer.parseInt(request.getParameter("month"));
		int day = Integer.parseInt(request.getParameter("day"));
		Date date = new Date(year-1900,month-1,day);
		int[] emotionDistribution = tweetEmotionDAO.getEmotionDistribution(trend, date);
		JSONArray newsArray = new JSONArray();
		for(int emotion : emotionDistribution){
			newsArray.add(emotion);
		}
		try {
			response.getWriter().print(newsArray);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return null;
	}

	public String getNews(){
		response.setContentType(" text/xml ");
		String trend = request.getParameter("trend");
		int year = Integer.parseInt(request.getParameter("year"));
		int month = Integer.parseInt(request.getParameter("month"));
		int day = Integer.parseInt(request.getParameter("day"));
		Date date = new Date(year-1900,month-1,day);
		ArrayList<NewsInfo> newsList = newsDAO.getNewsAboutTrend(trend, date);
		JSONArray newsArray = new JSONArray();
		for(NewsInfo news : newsList){
			JSONObject newsObject = new JSONObject();
			newsObject.put("url", news.getNewsURL());
			newsObject.put("content", news.getNewsContent().substring(0,(news.getNewsContent().length() > 300) ? 300 : news.getNewsContent().length())+"...");
			newsArray.add(newsObject);
		}
		try {
			response.getWriter().print(newsArray);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return null;
	}

	public String getKeywords(){
		response.setContentType(" text/xml ");
		String trend = request.getParameter("trend");
		int year = Integer.parseInt(request.getParameter("year"));
		int month = Integer.parseInt(request.getParameter("month"));
		int day = Integer.parseInt(request.getParameter("day"));
		Date date = new Date(year-1900,month-1,day);
		Map<String,Integer>frequencyList = newsKeywordsDAO.getFrequencyByTrend(trend, date);
		ArrayList<String> keywordsList = KeywordUtil.getTopKeywords(10, frequencyList);
		JSONArray keywordsArray = new JSONArray();
		for(String keyword : keywordsList){
			keywordsArray.add(keyword);
		}
	
		try {
			response.getWriter().print(keywordsArray);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return null;
	}	
	
}
